//MINI UI解析
mini.parse();

//导航配置处理器类
function EditNavCfgHandler() {
	this.data = {};
	this.type = mini.get("sncType");
	this.content = mini.get("sncContent");
	this.name = mini.get("sncName");
	this.order = mini.get("sncOrder");
	this.page = mini.get("sncPage");
	this.position = mini.get("sncPosition");
	this.display = mini.get("sncDisplay");
	this.icon = mini.get("sncIcon");
	this.url = mini.get("sncUrl");
	this.func = mini.get("sncFunction");
	this.state = mini.get("sncState");
	this.form = new mini.Form("editNavCfgForm");
	
	this.wmc = new WebMgrConfig();
	
	this.find = $("#find");
	this.find.attr({src : __root + "/admin/resource/css/ico/search.gif", onclick : "instance.search();"});
	
	this.load = function(data) {
		data = mini.clone(data);
		this.data.sncId = data.sncId;
		
		if (!data.isedit) {
			this.readonly();
			mini.get("save").hide();
		}
		
		this.initialize();
		
		if (data.sncId) {
			var param = { sncId : data.sncId };
			var json = mini.encode(param);
			$.ajax({
				url : __root + "/getNavConfig.do", 
				type : "POST", 
				data : { data : json }, 
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.reload(result.message.sncType);
						instance.form.setData(result.message);
						
						if (!data.isedit) {
							instance.find.hide();
						}
					} else {
						instance.readonly();
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					instance.readonly();
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.save = function() {
		this.form.validate();
		if (this.form.isValid()) {
			var data = this.form.getData();
			data.sncId = this.data.sncId;
			
			var json = mini.encode(data);
			$.ajax({
				url : __root + "/saveNavConfig.do", 
				type : "POST", 
				data: {
		        	data: json,
		        	lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
		        },
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.close("save");
					} else {
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.close = function(action) {
		if (CloseOwnerWindow) {
			CloseOwnerWindow(action);
		} else {
			window.close();
		}
	};
	
	this.search = function() {
		miniWin.afreshNewWin();
		miniWin.setParams(600, 350, "搜索内容");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("searchContent.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { type : instance.type.getValue(), request : "/getNavContents.do" };
	        iframe.contentWindow.instance.load(data, instance.selected);
		});
		
		miniWin.show();
	};
	
	this.selected = function(content) {
		instance.content.setValue(content);
	};
	
	this.initialize = function() {
		var typeData = mini.clone(this.wmc.navType);
		typeData.removeAt(0);
		this.type.setData(typeData);
		
		var pageData = mini.clone(this.wmc.normalPage);
		pageData.removeAt(0);
		this.page.setData(pageData);
		
		var positionData = mini.clone(this.wmc.navPosition);
		positionData.removeAt(0);
		this.position.setData(positionData);
		
		var stateData = mini.clone(this.wmc.normalState);
		stateData.removeAt(0);
		this.state.setData(stateData);
	};
	
	this.reload = function(type) {
		this.content.enable();
		this.content.setValue(null);
		this.content.setUrl(__root + "/getNavContent.do?type=" + type);
		
		if ((type == this.wmc.navType[1].value) || 
			(type == this.wmc.navType[5].value)) {
			this.content.disable();
			this.content.setValue("-");
			this.find.hide();
		} else if (type == this.wmc.navType[2].value) {
			this.find.hide();
		} else {
			this.find.show();
		}
	};
	
	this.readonly = function() {
		this.type.readOnly = true;
		this.content.readOnly = true;
		this.name.readOnly = true;
		this.order.readOnly = true;
		this.page.readOnly = true;
		this.position.readOnly = true;
		this.display.readOnly = true;
		this.icon.readOnly = true;
		this.url.readOnly = true;
		this.func.readOnly = true;
		this.state.readOnly = true;
	};
};

//导航配置处理器全局实例
var instance = new EditNavCfgHandler();

//定义内容类型值改变事件处理程序
function onSncTypeValueChanged(e) {
	var type = instance.type.getValue();
	instance.reload(type);
}

//定义保存按钮点击事件处理程序
function onSaveClick(e) {
	instance.save();
}

//定义关闭按钮点击事件处理程序
function onCancelClick(e) {
	instance.close("cancel");
}
